In [1]:
import numpy as np
import scipy as sp
import pandas as pd
In [2]:
sca = 24
sca
Out[2]:
In [3]:
vec = np.array([1, 12, 34])
vec
Out[3]:
In [4]:
np.ones((3,3))
Out[4]:
In [5]:
np.zeros((4,2))
Out[5]:
In [6]:
np.diag([1, 23])
Out[6]:
In [7]:
np.eye(2)
Out[7]:
In [8]:
sym = np.matrix('2,3,1;3,4,-1;1,-1,1')
sym
Out[8]:
In [9]:
sym.T
Out[9]:
In [10]:
ssym = np.matrix('2,3,1;-3,4,1;-1,-1,1')
ssym
Out[10]:
In [11]:
ssym.T
Out[11]:
In [12]:
mat = np.array([[10,5,9],
[2,20,6],
[8,3,30]]).reshape(3,3)
mat
Out[12]:
In [13]:
np.triu(mat,1)
Out[13]:
In [14]:
np.tril(mat, -1)
Out[14]:
In [15]:
vec1 = pd.Series([1, 2, 3, 4])
vec2 = pd.Series([2, 3, 40, 55])
vec1 + vec2
Out[15]:
In [16]:
mat1 = pd.DataFrame([[1, 23, 5], [55, 21, 96]])
mat2 = pd.DataFrame([[11, 43, 45], [115, 81, 23]])
mat1 + mat2
Out[16]:
In [17]:
sca = 5
vec = pd.Series([1, 2, 5, 10])
sca * vec
Out[17]:
In [18]:
mat1 = pd.DataFrame([[1, 23, 5], [55, 21, 96]])
mat2 = pd.DataFrame([[11, 43], [115, 81], [5, 1]])
mat1.dot(mat2)
Out[18]:
In [19]:
v1 = pd.Series([3, -1, 2])
v2 = pd.Series([2, 4, -1])
angle = np.arccos((v1.dot(v2))/np.linalg.norm(v1)*np.linalg.norm(v2))
# Angle in degrees
angle * 180/np.pi
Out[19]:
In [20]:
mat = pd.DataFrame(np.arange(1,10).reshape(3,-1).T)
vec = pd.Series([1, -1, 2])
print(mat)
vec
Out[20]:
In [21]:
vec * mat
Out[21]:
In [22]:
vec.dot(mat)
Out[22]:
In [23]:
mat.dot(vec)
Out[23]:
In [24]:
mat = pd.DataFrame([[0, 1, 0, 0, 1],
[0, 0, 1, 1, 0],
[0, 0, 0, 1, 0],
[0, 1, 0, 0, 0],
[0, 0, 0, 1, 0]])
mat.dot(mat)
Out[24]:
In [25]:
I1 = np.matrix('1, 0, 0; 0, 0, 1;0, 1, 0')
mat = pd.DataFrame(np.arange(1,10).reshape(3,-1).T)
print(I1)
print(mat)
In [26]:
# Pre-multiplying
I1.dot(mat) # resulted into exchanging of row 2 and 3
Out[26]:
In [27]:
# Post-multiplying
mat.dot(I1) # resulted into exchanging of column 2 and 3
Out[27]:
In [28]:
v1 = np.array([2, 3])
v2 = np.array([4, 4])
v2.dot((v2.T.dot(v1)/v2.T.dot(v2)))
Out[28]:
In [29]:
np.linalg.det([[1, 2],
[3, 4]])
Out[29]:
In [30]:
np.linalg.inv([[1, 2],
[3, 4]])
Out[30]:
In [31]:
ortho = np.matrix([[1/np.sqrt(2), 1/np.sqrt(2), 0],
[0, 0, 1],
[1/np.sqrt(2), -1/np.sqrt(2), 0]])
ortho
Out[31]:
In [32]:
ortho*ortho.T
Out[32]:
In [33]:
np.linalg.matrix_rank([[1, 2], [3, 4]])
Out[33]:
In [34]:
# Stretching a vector by scale 2
2 * np.array([2, 23])
Out[34]:
In [35]:
# Stretching a matrix by scale 2.5
2.5 * np.matrix([[1, 2], [3, 4]])
Out[35]:
In [36]:
coord = np.matrix([[-1, -2, -3],
[1, 2, 1]])
coord
Out[36]:
In [37]:
np.matrix([[1, 0],
[0, -1]]) * coord
Out[37]:
In [38]:
np.matrix([[-1, 0],
[0, 1]]) * coord
Out[38]:
In [39]:
np.matrix([[-1, 0],
[0, -1]]) * coord
Out[39]:
In [40]:
np.matrix([[0, 1],
[1, 0]]) * coord
Out[40]:
In [41]:
np.matrix([[0, 1],
[-1, 0]]) * coord
Out[41]:
In [42]:
np.matrix([[1, 0],
[0, 0]]) * coord
Out[42]:
In [43]:
np.matrix([[0, 0],
[0, 1]]) * coord
Out[43]:
In [44]:
mat = np.array([1, 3, -1, 4, 2, 5, 4, 19, 2, 3, -1, 7]).reshape(3,-1)
mat
A = mat[:,:3]
B = mat[:,3]
In [45]:
np.linalg.solve(A,B)
Out[45]:
In [46]:
mat = np.array([1, 0, 0, 1, 0, 1, 0, 2, 0, 0, 1, 3, 1, 1, 1, 6]).reshape(4,-1)
mat
A = mat[:,:3]
B = mat[:,3]
In [47]:
np.linalg.lstsq(A,B)[0]
Out[47]:
In [48]:
mat = np.matrix([[2, np.sqrt(2)], [np.sqrt(2), 1]])
mat
Out[48]:
In [49]:
np.linalg.eig(mat)[0]
Out[49]:
In [50]:
np.linalg.eig(mat)[1]
Out[50]:
In [51]:
mat = np.arange(1,10).reshape(3,-1).T
mat
Out[51]:
In [52]:
# Eigen values
np.round(np.linalg.eig(mat)[0]) + 0 # Addiing 0 to remove extra minus sign
Out[52]:
In [53]:
# Diagonal values are Eigenvalues
np.round(np.linalg.inv(np.linalg.eig(mat)[1]).dot(mat).dot(np.linalg.eig(mat)[1])) + 0
Out[53]:
In [54]:
mat = np.matrix([11, 20, 13, 1, 15, 6, 7, 8, 90]).reshape(3,-1)
np.linalg.eig(mat)[0] # All eigenvalues are greater than 0, hence "mat" is a Positive Definite matrix
Out[54]:
In [55]:
np.linalg.qr(mat)
Out[55]:
In [56]:
np.linalg.cholesky(mat)
Out[56]:
In [57]:
np.linalg.svd(mat)
Out[57]: